package com.company.project.wechat.weChatPay.controller;

import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.wechat.weChatPay.service.WeChatPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 第三方支付回调接口
 * @author ZYF
 * @date 2021/5/18 11:03
 */
@Api(tags = "第三方支付回调接口", description = "微信调用")
@RestController
@RequestMapping("/wxApi/payment")
public class PayCallBackController {

    @Autowired
    private WeChatPayService wxPayService;

    @RequestMapping(value = "/wxpay/notify", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "微信支付回调")
    @LogAnnotation(title = "微信支付回调", action = "微信支付回调")
    public String wxNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            return wxPayService.callBack(request, response);
        } catch (Exception e) {
            response.setHeader("Content-type", "application/xml");
            return "<xml>\n" +
                    "  <return_code><![CDATA[FAIL]]></return_code>\n" +
                    "  <return_msg><![CDATA[]]></return_msg>\n" +
                    "</xml>";
        }
    }

    @ApiOperation(value = "微信退款回调")
    @LogAnnotation(title = "微信退款回调", action = "微信退款回调")
    @RequestMapping(value = "/wxpay/wxRequestNotify", method = {RequestMethod.POST, RequestMethod.GET})
    public String wxRequestBack(HttpServletRequest request, HttpServletResponse response) {
        try {
            return wxPayService.wxRequestBack(request, response);
        } catch (Exception e) {
            response.setHeader("Content-type", "application/xml");
            return "<xml>\n" +
                    "  <return_code><![CDATA[FAIL]]></return_code>\n" +
                    "  <return_msg><![CDATA[]]></return_msg>\n" +
                    "</xml>";
        }
    }


}
